
/**
 * sysPermissionServiceImpl
 * 项目名称:分秒金融
 *
 * @author lunli
 *
 * 功能描述:资源管理管理
 * 
 * 历史版本:2017-03-28 v1.0.0 lunli  创建:
 * 
 */
package com.fmjr.sys.sysPermission.service.impl;

import com.fmjr.common.base.exception.BusinessException;
import com.fmjr.common.base.util.Pager;
import com.fmjr.common.util.SqlUtil;
import com.fmjr.sys.sysPermission.bean.SysPermission;
import com.fmjr.sys.sysPermission.dao.SysPermissionDao;
import com.fmjr.sys.sysPermission.service.ISysPermissionService;
import com.fmjr.sys.sysRolePermission.bean.SysRolePermission;
import com.fmjr.sys.sysRolePermission.dao.SysRolePermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;


@Service("sysPermissionService")
public class  SysPermissionServiceImpl implements ISysPermissionService {
	

	@Autowired
    private SysPermissionDao sysPermissionDao;
	@Autowired
	private SysRolePermissionDao sysRolePermissionDao;

	/**
	 * 根据条件查询资源管理记录
	 * @author lunli
	 * @param  pager
	 * @return SysPermissionList
	 * @throws BusinessException
	 */
	public void selectSysPermissionList(Pager<SysPermission> pager) throws BusinessException{
		int totalCount = sysPermissionDao.selectSysPermissionCount(pager);
		int numPerPage = SqlUtil.checkPageSize(pager.getNumPerPage());
		int currentPage = SqlUtil.checkPageCurrent(totalCount, numPerPage, pager.getPageNum());
		pager.setLimitStart(SqlUtil.countOffset(currentPage, numPerPage));
		pager.setPageSize(numPerPage);
		pager.setCurrentPage(currentPage);
		pager.setTotalCount(totalCount);
		pager.setTotalPage(totalCount%numPerPage==0?totalCount/numPerPage:totalCount/numPerPage+1);
		List<SysPermission> list = sysPermissionDao.selectSysPermissionList(pager);
		pager.setList(list);
	}
	
	/**
	 * 根据ID查询资源管理记录
	 * @author lunli
	 * @param id
	 * @return domain
	 * @throws BusinessException
	 */
	public SysPermission selectSysPermissionById(Long id) throws BusinessException{
		return sysPermissionDao.selectSysPermissionById(id);
	}

	/**
	 *  删除资源管理记录
	 * @author lunli	 
	 * @param id
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int deleteSysPermissionById(Long id) throws BusinessException{
		return sysPermissionDao.deleteSysPermissionById(id);
	}
	
	/**
	 * 修改资源管理记录
	 * @author lunli
	 * @param sysPermission
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int updateSysPermission(SysPermission sysPermission) throws BusinessException{
		return sysPermissionDao.updateSysPermission(sysPermission);
	}

	/**
	 * 新增资源管理记录
	 * @author lunli
	 * @param sysPermission
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int createSysPermission(SysPermission sysPermission) throws BusinessException{
		return sysPermissionDao.createSysPermission(sysPermission);
	}

	@Override
	public List<SysPermission> searchPermissionByRoleId(Long roleId) throws BusinessException {
		List<SysRolePermission> listRolePermission = sysRolePermissionDao.searchRolePermissionByRoleId(roleId);
		if(listRolePermission == null || listRolePermission.size() == 0){
			return null;
		}
		List<Long> listPids = new LinkedList<Long>();
		for(int i=0;i<listRolePermission.size();i++){
			listPids.add(listRolePermission.get(i).getSysPermissionId());
		}
		return sysPermissionDao.searchPermissionByIds(listPids);
	}

	@Override
	public List<SysPermission> getSysPermissionAll() throws BusinessException {
		return sysPermissionDao.getSysPermissionAll();
	}
}
